﻿#pragma once
#include <QObject>
#include <QThread>
#include <QMutex>
#include <QQueue>
#include "tcp/tcpclient.h"

QByteArray ToBin(int val, int size);
QByteArray ToBin(float vale, int size);

unsigned int FromBinToInt(const QByteArray& d);
float FromBinToFloat(const QByteArray& d);
QString FromBinToString(const QByteArray& d);

class StatusThread : public QThread
{
	Q_OBJECT

public:
	explicit StatusThread(QObject* parent = nullptr)
		: QThread(parent), m_running(false) {
		moveToThread(this);
	}
	bool Init(const QString& ip, const int port);
	void Stop();

signals:
	void statusUpdated(const QByteArray& status);
	void connectionStatusChanged(int connected);

private slots:
	void OnDisConnected();
	void OnReceievedData(const QByteArray& data);
	void OnStatusReciveTimeout();
protected:
	void run() override;
private:
	void cleanupResources();
private:
	TCPClient* m_socket = nullptr;
	QByteArray m_buffer;
	bool m_running;
	QString m_ip;
	int m_port;
	QTimer* m_timer = nullptr;
};
